package com.lxg.springboot.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * @version V3.0
 * @Title: ApiUtil
 * @Company: 成都影达科技有限公司
 * @Description: 描述
 * @author: 东进
 * @date 2018/10/29 下午4:54
 */
public class ApiUtil {

    public static String getSign(String secret, String message) {
        try {
            System.out.println(message);
            SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(signingKey);
            return byte2hex(mac.doFinal(message.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String byte2hex(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b != null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toLowerCase();
    }

//    public static void main(String[] args) {
//        Long longTime = Common.getCurrentDateLongTime();
//        System.out.println(longTime);
//    }
}
